package boundary;

import controler.ConnectionManager;
import controler.CreateTables;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class InserirPacotesServlet
 */
@WebServlet("/InserirPacoteServlet")
public class InserirPacoteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public InserirPacoteServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		DecimalFormat dff = (DecimalFormat) DecimalFormat.getInstance();
		
		String preco = request.getParameter("preco");
		// cast para double dentro de TRY.
		String origem = request.getParameter("origem");
		String destino = request.getParameter("destino");
		Integer ida = Integer.parseInt(request.getParameter("ida"));
		Integer volta = Integer.parseInt(request.getParameter("volta"));
		Integer classificacaohotel = Integer.parseInt(request.getParameter("classificacaohotel"));
		String nomehotel = request.getParameter("nomehotel");
		Integer companhiaaereaid = Integer.parseInt(request.getParameter("companhiaaereaid"));
		
		try {
			System.out.println("PASSO AQUILKSDAFH");
			CreateTables.Create("./bd");
			ConnectionManager.connect("./bd");
			System.out.println("PASSO AQUI");
			try {
				PreparedStatement sql = ConnectionManager.getConnection().prepareStatement("INSERT INTO pacotes (preco, origem, destino, ida, volta, classificacaohotel, nomehotel, companhiaaereaid) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
				Integer preco2 = Integer.parseInt(preco);
				sql.setInt(1, preco2);
	           	sql.setString(2, origem);
	           	sql.setString(3, destino);
	           	sql.setInt(4, ida);
	           	sql.setInt(5, volta);
	           	sql.setInt(6, classificacaohotel);
	           	sql.setString(7, nomehotel);
	           	sql.setInt(8, companhiaaereaid);
	            	
	           	sql.execute();
	           	
	           	ConnectionManager.disconnect();
			}
			catch (SQLException e){
				System.out.println("Can't add the values to the derby database: " + e.getMessage());
			}
		}
		catch (SQLException e){
			System.out.println("Can't connect to the derby database: " + e.getMessage());
		}
		catch (ClassNotFoundException e) {
			System.out.println("Missing class! Check your CLASSPATH" + e.getMessage());
		}

		response.sendRedirect("./index.jsp?s=sc");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
